package com.liu.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.seckill.mapper.SeckillOrderMapper;
import com.liu.seckill.pojo.SeckillOrder;
import com.liu.seckill.pojo.User;
import com.liu.seckill.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @ClassName SeckillOrderServiceImpl
 * @Author ：name
 * @Date ：2022-02-16 15:59
 * @Description：
 * @Version: 1.0
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
  private SeckillOrderMapper seckillOrderMapper;
  @Autowired
  private RedisTemplate redisTemplate;

    @Override
    public Long getResult(User user, Long goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>().eq("user_id", user.getId())
                .eq("goods_id", goodsId));

        if (null!=seckillOrder){
            return seckillOrder.getOrderId();
        }else{
            if (redisTemplate.hasKey("isStockEmpty:"+goodsId)){
                return -1L;
            }else{
                return 0L;
            }
        }



    }
}
